const Pagination = {
    data: () => ({
        pageNumber: 1,
        pageMeta: {},
        pageItems: [],
        pageHasNoMore: false
    }),
    methods: {
        async initPage() {
            this.pageNumber = 1
            this.pageHasNoMore = false
            this.pageMeta = {}
            this.pageItems = []
        },
        async loadPageData() {
            const response = await this.loadPageItems()
					
						if(this.isActivityAccount || this.isActivityGoldCoins || this.isActivityGoldCashes){
							if(response){
								if (this.pageNumber === 1) {
								    this.pageItems = response.data.data
								} else {
								    this.pageItems = [...this.pageItems, ...response.data.data]
								}
								const meta = response.data;
								this.pageMeta = meta;
								this.total_num = response.total;
								if (meta.current_page === meta.last_page) {
								    this.pageHasNoMore = true
								}
							}
						}else{
							
							if(response&&response.data){
								
								if (this.pageNumber === 1) {
								    this.pageItems = response.data
								} else {
								    this.pageItems = [...this.pageItems, ...response.data]
								}
								const meta = response.meta;
								this.pageMeta = meta;
								if (meta.current_page === meta.last_page) {
								    this.pageHasNoMore = true
								}
							}else{
								if (this.pageNumber === 1) {
								    this.pageItems = response
								} else {
								    this.pageItems = [...this.pageItems, ...response]
								}
							}
						}
						
						console.log(this.pageItems);	
        }
    },
    async onPullDownRefresh() {
			console.log('refresh');
				if(!this.isActivityList&&!this.isActivityAccount&&!this.isActivityGoldCoins&&!this.isActivityGoldCashes){
					await this.initPage()
					await this.loadPageData()
				}

				if(this.isActivity){
					this.activityRounds = []
					this.goldRounds()
					this.$forceUpdate()
					await this.getUser()
				}

				if(this.isActivityRecord){
					this.records = []
					this.needGoldbeans = 0
					this.totalMoney = 0
					this.isAllChecked = false
					this.isWaitingAllChecked = false
					if(this.paidState == 'unpaid'){
						this.init()
						this.$forceUpdate()
					}
				}
				
				if(this.isActivityAccount){
					if(this.datetimerange.length!=0){
						this.$refs.datetimePicker.clear()
					}else{
						await this.initPage()
						await this.loadPageData()
					}
					this.getUser()
				}
				
				if(this.isActivityGoldCoins){
					if(this.datetimerange.length!=0){
						this.$refs.datetimePicker.clear()
					}else{
						await this.initPage()
						await this.loadPageData()
					}
					this.getUser()
				}
				
				if(this.isActivityGoldCashes){
					if(this.datetimerange.length!=0){
						this.$refs.datetimePicker.clear()
					}else{
						await this.initPage()
						await this.loadPageData()
					}
					this.getUser()
				}
				
				// if(this.isActivityList){
				// 	this.timeData = {}
				// 	this.isStart = false
				// 	this.isCanhow = false
				// 	this.activityRounds = []
				// 	this.goldRounds()
				// 	this.goldProducts()
				// }
				
        uni.stopPullDownRefresh()
    },
    async onReachBottom() {
        if (this.pageHasNoMore) return
        this.pageNumber += 1
        await this.loadPageData()
				if(this.isActivityRecord){
					if(this.isAllChecked){
						this.loadingBySelectAll()
					}
					if(this.isWaitingAllChecked){
						this.loadingBySelectAllWaitingHandle()
						this.$forceUpdate()
					}
					if(this.paidState == 'unpaid'){
						this.init()
						this.$forceUpdate()
					}
				}
    }
	
}

export default Pagination
